<template>
    <div class="right-login-box">
        <el-form :model="form" :rules="rules" ref="form" label-position="top">
            <el-form-item label="用户名" prop="username">
                <el-input size="mini" v-model="form.username" auto-complete="off" clearable placeholder="用户名/邮箱"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password" v-if="!isCode">
                <el-input size="mini" type="password" v-model="form.password" show-password auto-complete="off"
                    placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item label="验证码" prop="code" v-if="isCode">
                <el-input size="mini" v-model="form.code" auto-complete="off" placeholder="请输入验证码">
                    <template slot="append">
                        <el-button type="text" size="mini" @click="getCode" :disabled="timeScend != 60">{{ timeScend != 60 ?
                            timeScend + 's后重试' : '获取验证码' }}</el-button>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item class="btn-box">
                <el-checkbox size="mini" :disabled="isCode" v-model="isRemember" @change="rememberPwd">记住密码</el-checkbox>
                <el-button type="text" size="mini" @click="forgetPwd">忘记密码</el-button>
            </el-form-item>
            <el-form-item class="btn-submit-box">
                <el-button size="mini" type="primary" @click="submitForm('form')">登录</el-button>
            </el-form-item>
            <el-form-item class="btn-box">
                <el-button type="text" size="mini" @click="signUp">还没账号？注册</el-button>
                <el-button type="text" size="mini" @click="isCode = !isCode">
                    {{ isCode ? '密码登录' : '验证码登录' }}
                </el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    name: 'right-login',
    props: {
        timeScend: {
            type: Number,
            default: 60
        },
        rememberInfo: {
            type: Object,
            default: () => {
                return {
                    username: '',
                    password: ''
                }
            }
        }
    },
    data() {
        return {
            form: {
                username: '',
                password: '',
                code: ''
            },
            isCode: false,
            isRemember: false
        }
    },
    computed: {
        rules() {
            return {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password: [
                    { required: !this.isCode, message: '请输入密码', trigger: 'blur' }
                ],
                code: [
                    { required: this.isCode, message: '请输入验证码', trigger: 'blur' },
                    { min: 6, max: 6, message: '验证码长度为6', trigger: 'blur' }
                ]
            }
        }
    },
    watch: {
        rememberInfo: {
            handler(val) {
                this.form.username = val.username;
                this.form.password = val.password;
                if (val.username && val.password) { this.isRemember = true; }
            },
            deep: true
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$emit('submit', this.form);
                } else {
                    return false;
                }
            });
        },
        signUp() {
            this.$emit('signUp');
        },

        /**获取验证码*/
        getCode() {
            this.$emit('getCode', this.form.username);
        },
        /**记住密码*/
        rememberPwd() {
            this.$emit('rememberPwd', this.isRemember, this.form);
        },
        forgetPwd() {
            this.$emit('forgetPwd');
        }

    }
}
</script>
<style lang="scss" scoped>
.right-login-box {
    background-color: #fff;
    border: #fff;
    margin: 0 auto 20px;
    padding: 16px;
    box-shadow: 0 4px 8px 6px rgba(7, 17, 21, 0.06);
    border-radius: 8px;
    cursor: pointer;
    position: relative;
    overflow: hidden;

    &:hover {
        box-shadow: 0 4px 8px 10px rgba(7, 17, 21, 0.1);
    }

    /deep/.el-button--primary {
        background-color: var(--themeColor);
        border-color: var(--themeColor);
    }

    /deep/.el-form-item {
        margin-bottom: 0px;

        .el-form-item__label {
            line-height: normal;
            padding-bottom: 5px;
        }

        .el-form-item__error {
            top: -23px;
            left: auto;
            right: 10px;
        }

        .el-form-item__content {
            line-height: normal;
        }
    }

    .btn-submit-box {
        text-align: center;
        // margin-top: 10px;

        .el-button {
            width: 100%;
        }
    }

    .btn-box {

        // text-align: center;
        /deep/.el-form-item__content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            width: 100%;

            &::before {
                display: none;
            }

            &::after {
                display: none;
            }

            .el-checkbox {
                .el-checkbox__label {
                    font-size: 12px;
                }
            }
        }
    }
}</style>